<?php
namespace Act\Os\Ajax;
use \Act\Os\Backend;
class Shortcut extends Backend{
	public function doAdd(){
		$menu_id = get('menu_id',0,'absint');
		$current_user_info = session('user_info');
		$user_id = $current_user_info['user_id'];
		$shortcuts = $current_user_info['shortcuts'];
		$shortcut_arr = explode(',',$shortcuts);

		if(!in_array($menu_id,$shortcut_arr)){
			$shortcut_arr[]=$menu_id;
			asort($shortcut_arr);
		}
		$shortcuts = implode(',',$shortcut_arr);
		$update_data = array ('shortcuts' => $shortcuts );
			
		$result = model('#User','adm_')->updateUser($user_id,$update_data );
		if($result !==false ){
			$ret = json_encode(array("result"=>"1","msg"=>"添加成功"));
			model('#User','adm_')->reloadSession();
		}else{
			$ret = json_encode(array("result"=>"0","msg"=>"操作失败!"));
		}
		echo $ret;
	}

	public function doDelete(){
		$menu_id = get('menu_id',0,'absint');
		$current_user_info = session('user_info');
		$user_id = $current_user_info['user_id'];
		$shortcuts = $current_user_info['shortcuts'];
		$shortcut_arr = explode(',',$shortcuts);
		$shortcut_arr = explode(',',$shortcuts);
		$idx = array_search($menu_id,$shortcut_arr);
		if($idx !==false ){
			unset($shortcut_arr[$idx]);
		}
		$shortcuts = implode(',',$shortcut_arr);
		$update_data = array ('shortcuts' => $shortcuts );
			
		$result = model('#User','adm_')->updateUser ( $user_id,$update_data );
		if($result !==false ){
			$ret = json_encode(array("result"=>"1","msg"=>"删除成功"));
			model('#User','adm_')->reloadSession();
		}else{
			$ret = json_encode(array("result"=>"0","msg"=>"操作失败!"));
		}
		echo $ret;
	}
}